<?php
/**
 * 系统缓存生成类
 *
 */

class Cache {

	//缓存文件名
	public $data_list = array(
					'sys' => 'sys/cache_system_info.php',
					'cl' => 'sys/cache_article_category_info.php',
					'gl' => 'sys/cache_goods_category_info.php',
				);
	
	//缓存内容开始
	public $cache_file_header = "<?php\n//" ;

	//缓存内容结束
	public $cache_file_footer = "\n?>";
	
	/**
	 * 清空缓存
	 *
	 * @param string $key  缓存文件缩写
	 */
	function cleanCache($key)
	{
		unlink(CACHE_DIR . $this->data_list[$key]);
	}
	
	function getCache($key)
	{
		return $this->data_list[$key];
	}
	
	/**
	 * 判断缓存是否存在
	 *
	 * @param string $key 缓存文件缩写
	 * @return boolen
	 */
	function existsCache($key)
	{
		return file_exists(CACHE_DIR . $this->data_list[$key]);
	}

	/**
	 * 生成缓存
	 *
	 * @param unknown_type $key
	 */
	function makeCache($key)
	{
		global $sys_conf_var,$db;

		switch ($key) {
			case 'sys':

				$recordSet = $db->Execute("SELECT * FROM " . $sys_conf_var['table']['system']);

				if(!$recordSet) {
					print $db->ErrorMsg();
					exit;
				}else {
					while (!$recordSet->EOF) {
						$sys_info[$recordSet->fields['Var']] = $recordSet->fields['Value'];
						$recordSet->MoveNext();
					}
				}
				
				$results = var_export ($sys_info, true);
				$results = '$sys_info = '.$results.";";
				$this->writeCache($key,$results);
				break;
			case 'cl':
                $record_set = $db->Execute("SELECT *  FROM " . $sys_conf_var['table']['article_category'] . " ORDER BY `ID` ASC");
			
                if(!$record_set) 
				{
					print $db->ErrorMsg();
					exit;
				}else {
					while (!$record_set->EOF) 
					{
                    	$record[ID] = $record_set->fields[ID];
                        $record[CategoryName] = $record_set->fields[CategoryName];
                        $record[CategoryCode] = $record_set->fields[CategoryCode];
                        $record[ParentCategoryID] = $record_set->fields[ParentCategoryID];
                        $record[Disable] = $record_set->fields[Disable];
						$article_category_list[$record_set->fields[ID]] = $record;
						$record_set->MoveNext();
					}
				}
				$results = var_export($article_category_list, true);
				$results = '$article_category_list = '.$results.";";
				$this->writeCache($key,$results);
				break;
			case 'gl':
                $record_set = $db->Execute("SELECT *  FROM " . $sys_conf_var['table']['goods_category'] . " ORDER BY `ID` ASC");
			
                if(!$record_set) 
				{
					print $db->ErrorMsg();
					exit;
				}else {
					while (!$record_set->EOF) 
					{
                    	$record[ID] = $record_set->fields[ID];
                        $record[CategoryName] = $record_set->fields[CategoryName];
                        $record[CategoryCode] = $record_set->fields[CategoryCode];
                        $record[ParentCategoryID] = $record_set->fields[ParentCategoryID];
                        $record[Disable] = $record_set->fields[Disable];
						$goods_category_list[$record_set->fields[ID]] = $record;
						$record_set->MoveNext();
					}
				}
				$results = var_export($goods_category_list, true);
				$results = '$goods_category_list = '.$results.";";
				//echo $results;exit;
				$this->writeCache($key,$results);
				break;

		}
	}

	/**
	 * 写缓存文件
	 *
	 * @param string $key 缓存文件缩写
	 * @param string $cache_data 缓存内容
	 */
	function writeCache($key,$cache_data)
	{
		$cache_data = $this->cache_file_header.date("F j, Y, H:i")."\n\n" . $cache_data . $this->cache_file_footer;
		$handle = fopen(CACHE_DIR . $this->data_list[$key],'w');
		@flock($handle,3);  //这里可以改为 读写均锁。
		fwrite($handle,$cache_data);
		fclose($handle);
	}
}
?>